package com.example.testrss;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class XMLParser_Nasa extends DefaultHandler {

	private static String LOG_TAG = "Test_RSS";
	
	ArrayList<NasaImageOfTheDay> itemList = null;
	NasaImageOfTheDay item = null;
	
	StringBuilder stringBuilder;
	
	@Override
	public void startDocument() throws SAXException {
//		super.startDocument();
		
		itemList = new ArrayList<NasaImageOfTheDay>();
	} // startDocument()
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
//		super.startElement(uri, localName, qName, attributes);
		
		stringBuilder = new StringBuilder();
		
		if(localName.equals("item")) {
			item = new NasaImageOfTheDay();
		}
	} // startElement()
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
//		super.endElement(uri, localName, qName);
		
		if(localName.equals("item")) {
			itemList.add(item);
		} else if(localName.equals("title")) {
			item.setTitle(stringBuilder.toString());
			Log.i(LOG_TAG, "title: " + item.getTitle());
		} else if(localName.equals("link")) {
			item.setLink(stringBuilder.toString());
			Log.i(LOG_TAG, "link: " + item.getLink());
		} else if(localName.equals("description")) {
			item.setDescription(stringBuilder.toString());
			Log.i(LOG_TAG, "description: " + item.getDescription());
		} else if(localName.equals("enclosure")) {
			Log.i(LOG_TAG, "endElement: enclosure");
			// TODO
		} else if(localName.equals("pubDate")) {
			item.setPubDate(stringBuilder.toString());
			Log.i(LOG_TAG, "pubDate: " + item.getPubDate());
		}
		
	} // endElement()
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
//		super.characters(ch, start, length);
		
		String temp = new String(ch, start, length);
		stringBuilder.append(temp);
		
	} // characters()
	
} // class XMLParser_Nasa
